package com.topvision.myviewtest.recycler.itemdecoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.topvision.myviewtest.R;

/**
 * @author lucy
 * @date 2018-03-28 15:21
 * @description //下划线Decoration
 */

public class DeviderDecoration extends RecyclerView.ItemDecoration {


    private final Paint mPaint;
    private final int mDeviderHeight;

    public DeviderDecoration(Context context) {
        // 设置画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // 颜色
        mPaint.setColor(Color.GREEN);
        // 分割线高度
        mDeviderHeight = context.getResources().getDimensionPixelSize(R.dimen.item_decoration_padding);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.bottom = mDeviderHeight;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();
        // 获取左右点
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            int bottom = view.getBottom() + mDeviderHeight;
            int top = view.getBottom();

            c.drawRect(left, top, right, bottom, mPaint);
        }
    }
}
